package main

import (
	"fmt"
	"time"
)

// now := time.Now()
// time.Time
// "2006-01-02 15:04:05"
func main() {
	testNow()
	testNowDetail()
	specifiedTime()
	testFormat01()
	testFormat02()
	testFormat03()
}

// 获取当前时间
func testNow() {
	fmt.Println("-------------获取当前时间-------------")
	now := time.Now()
	fmt.Printf("%T %s \n", now, now)

	var t time.Time = time.Now()
	fmt.Printf("%T %s \n", t, t)
}

// 获取当前时间的详细信息
func testNowDetail() {
	fmt.Println("-------------获取当前时间的详细信息-------------")
	now := time.Now()
	fmt.Printf("现在now=%s \n", now)
	fmt.Printf("年份Year=%d\n", now.Year())
	fmt.Printf("月份Month=%s\n", now.Month().String())
	fmt.Printf("月份Month=%v\n", int(now.Month()))
	fmt.Printf("日Day=%d\n", now.Day())
	fmt.Printf("小时Hour=%d\n", now.Hour())
	fmt.Printf("分钟Minute=%d\n", now.Minute())
	fmt.Printf("秒Second=%d\n", now.Second())
	fmt.Printf("时间戳Unix        =%d\n", now.Unix())
	fmt.Printf("毫秒时间戳UnixMilli=%d\n", now.UnixMilli())
	fmt.Printf("微妙时间戳UnixMicro=%d\n", now.UnixMicro())
	fmt.Printf("纳秒时间戳UnixNano =%d\n", now.UnixNano())
}

// specifiedTime
//
//	@Description: 指定时间
func specifiedTime() {
	fmt.Println("-------------指定时间-------------")
	date := time.Date(2024, time.October, 13, 22, 15, 03, 0, time.Local)
	location := date.Location()
	fmt.Printf("date=%s \n", date)
	fmt.Printf("location=%s \n", location)

	//加载时区
	loadLocation, err := time.LoadLocation("Asia/Shanghai")
	if err != nil {
		fmt.Println("loadLocation err=", err)
	} else {
		fmt.Printf("loadLocation=%s \n", loadLocation)
	}
}

// 使用fmt.Printf格式化时间
func testFormat01() {
	fmt.Println("-------------使用fmt.Printf格式化时间-------------")
	now := time.Now()

	fmt.Printf("%d-%d-%d %d:%d:%d \n", now.Year(), int(now.Month()), now.Day(), now.Hour(), now.Minute(), now.Second())
	fmt.Printf("%d/%d/%d %d:%d:%d \n", now.Year(), int(now.Month()), now.Day(), now.Hour(), now.Minute(), now.Second())
}

// 使用 fmt.Sprintf格式化时间
func testFormat02() {
	fmt.Println("-------------使用 fmt.Sprintf格式化时间-------------")

	var now time.Time = time.Now()
	fmt.Println(fmt.Sprintf("%d-%d-%d %d:%d:%d", now.Year(), int(now.Month()), now.Day(), now.Hour(), now.Minute(), now.Second()))
	fmt.Println(fmt.Sprintf("%d/%d/%d %d:%d:%d", now.Year(), int(now.Month()), now.Day(), now.Hour(), now.Minute(), now.Second()))
}

// 使用 func (t Time) Format(layout string) string 格式化时间
func testFormat03() {
	fmt.Println("-------------func (t Time) Format(layout string) string 格式化时间-------------")
	var now time.Time = time.Now()
	//记忆：2006年12345
	format := now.Format("2006-01-02 15:04:05")
	fmt.Println(format)

	format1 := now.Format("2006/01/02 15:04:05")
	fmt.Println(format1) //2024/11/13 22:15:03
	format2 := now.Format("2006/01/02")
	fmt.Println(format2) //2024/11/13
	format3 := now.Format("15:04:05")
	fmt.Println(format3) //22:15:03
	format4 := now.Format("2006")
	fmt.Println(format4) //2024
}
